/*
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2022-10-21 14:40:08
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2022-10-25 18:26:43
 * @FilePath: \v-shop\code\src\router\index.js
 * @Description: 配置路由表
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // 重定向
    redirect: "/welcome",
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/AboutView',
    name: 'AboutView',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/index.vue'),
    // 二级路由
    children: [
      {
        path: '/myaside',
        name: 'myaside',
        component: () => import('../views/myaside.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/users.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/roles.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/rights.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/goods.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/params.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/categories.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/orders.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/reports.vue')
      },
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('../views/welcome.vue')
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

//全局路由守卫的 前置钩子
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.token ? true : false;
  //1:判断是否为登陆注册
  if (to.path == "/login") {
    next();
  } else {
    isLogin ? next() : next("/login");
  }
});

export default router


